package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ArrangeLectureHistory;
import com.yuyou.fn.educational.mapper.ArrangeLectureHistoryMapper;
import com.yuyou.fn.educational.service.IArrangeLectureHistoryService;
import com.yuyou.fn.educational.vo.ArrangeLectureHistoryDetailVo;
import com.yuyou.fn.educational.vo.ArrangeLectureHistoryQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * ArrangeLectureHistory 表数据服务层接口实现类
 *
 */
@Service
public class ArrangeLectureHistoryServiceImpl extends SuperServiceImpl<ArrangeLectureHistoryMapper, ArrangeLectureHistory,Long> implements IArrangeLectureHistoryService {

    @Override
    public Page<ArrangeLectureHistoryDetailVo> findArrangeLectureHistoryPage(DataPermission dataPermission, ArrangeLectureHistoryQueryVo arrangeLectureHistoryQueryVo, int pageNo, int pageSize) {
        if (arrangeLectureHistoryQueryVo.getAreaTeamId()==null){
            throw new BusException("区域id必传");
        }
        if (arrangeLectureHistoryQueryVo.getBusTeamIds()==null){
            arrangeLectureHistoryQueryVo.setBusTeamIds(dataPermission.getBusTeamIds());
        }
        Page<ArrangeLectureHistoryDetailVo> page=new Page<>(pageNo,pageSize);
        if (arrangeLectureHistoryQueryVo.getBusTeamId()==null && !Utils.checkListNotEmpty(arrangeLectureHistoryQueryVo.getBusTeamIds())){
            page.setTotal(0);
            return page;
        }
        if (pageNo!=0){
            pageNo=pageNo-1;
        }
        Integer total=this.countArrangeLetureHistory(arrangeLectureHistoryQueryVo);
        List<ArrangeLectureHistoryDetailVo> arrangeLectureHistoryDetailVoList=this.findLimitList(arrangeLectureHistoryQueryVo,pageNo*pageSize,pageSize);

        page.setTotal(total);
        page.setRecords(arrangeLectureHistoryDetailVoList);
        return page;
    }



    @Override
    public List<ArrangeLectureHistoryDetailVo> findLimitList(ArrangeLectureHistoryQueryVo arrangeLectureHistoryQueryVo, int start, int end) {
        return this.autoMapper.findArrangeLectureHistoryLimitList(arrangeLectureHistoryQueryVo,start,end);
    }

    @Override
    public SXSSFWorkbook exportArrangeLectureHistory(DataPermission dataPermission, ArrangeLectureHistoryQueryVo arrangeLectureHistoryQueryVo) {
        if (arrangeLectureHistoryQueryVo.getAreaTeamId()==null){
            throw new BusException("区域id必传");
        }
        if (arrangeLectureHistoryQueryVo.getBusTeamId()==null){
            arrangeLectureHistoryQueryVo.setBusTeamIds(dataPermission.getBusTeamIds());
        }
        List<ArrangeLectureHistoryDetailVo> arrangeLectureHistoryDetailVoList = this.findLimitList(arrangeLectureHistoryQueryVo,0,50000);
        return arrangeLectureHistory2Excel(arrangeLectureHistoryDetailVoList);
    }

    public static SXSSFWorkbook arrangeLectureHistory2Excel(List<ArrangeLectureHistoryDetailVo> arrangeLectureHistoryDetailVoList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("上课时间调整记录");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{"期数","业务组","年级","学科","班级名称","班级编号","班级讲次 ","上课日期(原)","上课时间(原)","上课日期(现)","上课时间(现)","修改人","修改时间"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum=1;
        for (ArrangeLectureHistoryDetailVo arrangeLectureHistoryDetailVo:arrangeLectureHistoryDetailVoList){
            int cellNum = 0;
            SXSSFRow row = sheet.createRow(rowNum++);

            SXSSFCell cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getPeriodName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getBusTeamName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getGradeName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getSubjectName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getClassName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getClassNo()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(arrangeLectureHistoryDetailVo.getLectureNo());

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getOriginClassDate()));


            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getOriginStartTime())+"-"+StringUtils.defaultString(arrangeLectureHistoryDetailVo.getOriginEndTime()));


            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getClassDate()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getStartTime())+"-"+StringUtils.defaultString(arrangeLectureHistoryDetailVo.getEndTime()));


            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeLectureHistoryDetailVo.getOperatorName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(Utils.formatDate(arrangeLectureHistoryDetailVo.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));

        }
        return excel;
    }



    @Override
    public Integer countArrangeLetureHistory(ArrangeLectureHistoryQueryVo arrangeLectureHistoryQueryVo) {
        return this.autoMapper.countArrangeLectureHistory(arrangeLectureHistoryQueryVo);
    }
}